package spring.board.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.stereotype.Repository;

import spring.board.service.FriendlistService;
import spring.board.service.FriendlistServiceImp;
import spring.board.vo.FriendlistVO;
import spring.board.vo.UploadImageVO;

@Repository
public class UploadImageImple extends SqlSessionDaoSupport implements
		UploadImageDao {

	private Integer block;

	@SuppressWarnings("unchecked")
	@Override
	public List uploadList(String username) {
		List list = new ArrayList();
		UploadImageVO paging = new UploadImageVO();
		HashMap<String, Object> map = new HashMap<String, Object>();
		paging.setUsername(username);

		FriendlistVO flist = new FriendlistVO();
		flist.setUsername(username);
		flist = (FriendlistVO) getSqlSession().selectOne("main.Friendlist",
				flist);

		list = separation(flist.getFriend());
		list.add(username);
		map.put("friendlist", list);
		return getSqlSession().selectList("main.Paging", map);
	}

	@Override
	public int uploadBlock() {
		return block;
	}

	@Override
	public void uploadWrite(UploadImageVO upload) {
		getSqlSession().insert("main.boardAdd", upload);
	}

	@Override
	public UploadImageVO uploadView(String num) {
		return (UploadImageVO) getSqlSession().selectOne("main.boardDetail",
				num);

	}

	@Override
	public void uploadUpdate(UploadImageVO upload) {
		getSqlSession().update("main.boardUpdate", upload);
	}

	@Override
	public void uploadDelete(String num) {

		getSqlSession().delete("main.boardDelete", num);
	}

	public List separation(String sentence) {
		List list = new ArrayList();
		list.clear();
		try {
			String[] array = sentence.split(",");
			System.out.println(array.length);
			for (int i = 0; i < array.length; i++) {
				list.add(array[i]);
			}
			return list;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return list;
	}

}